package com.isunland.managesystem.utils;

import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/7/10 15:19
 */

public class WebViewUtil {

  public static void init(final BaseVolleyActivity context, final WebView mWv,
      final boolean showDialog) {
    if (mWv == null || context == null) return;
    MyUtils.syncCookie(context);
    WebSettings settings = mWv.getSettings();
    //设置可调用js.
    settings.setJavaScriptEnabled(true);
    mWv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    //final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
    mWv.setWebViewClient(new WebViewClient() {
      //在当前页面打开链接.
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }

      @Override public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        if (showDialog) MyUtils.showDialogLoading(context, context.getString(R.string.loading));
      }

      public void onPageFinished(WebView view, String url) {
        Log.i("", "Finished loading URL: " + url);
        MyUtils.dismissDialog();
      }

      @SuppressWarnings("deprecation")
      public void onReceivedError(WebView view, int errorCode, String description,
          String failingUrl) {
        Log.e("", "Error: " + description);
        mWv.loadUrl("file:///android_asset/error.html");
        //alertDialog.setTitle("错误");
        //alertDialog.setMessage("请刷新重试");
        //alertDialog.show();
      }
    });
  }
}
